package com.llmcu.source.security.shiro.shiro01;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * 第一个shiro例子
 *
 * @author liuling
 * @date 2024/12/30 6:35
 */
public class ShiroRun {
    public static void main(String[] args) {
        // 1.初始化获取SecurityManager
        IniSecurityManagerFactory factory =
                new IniSecurityManagerFactory( "classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        // 2.获取subject对象
        Subject subject = SecurityUtils.getSubject();
        // 3.创建token对象
        AuthenticationToken token = new UsernamePasswordToken("zhangsan","z3");
        // 4.完成登陆
        try{
            subject.login(token);
            System.out.println("登陆成功");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            System.out.println("密码错误");
        }catch (UnknownAccountException e1){
            e1.printStackTrace();
            System.out.println("用户名不存在");
        }catch (AuthenticationException e2){
            e2.printStackTrace();
            System.out.println("未知错误");
        }
    }

}
